package org.happen.android.thrid.icache.ui;

import java.io.Serializable;

import org.happen.android.R;
import org.happen.android.base.AppV4FragmentActivity;
import org.happen.android.module.TitleModule;
import org.happen.android.thrid.icache.ICaches;
import org.happen.android.thrid.icache.cache.ImageWorker;
import org.liushui.mycommons.android.util.McToastUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageDetailActivity extends AppV4FragmentActivity
{

	public static final String EXTRA_PARAM = "extra_position";

	public static class ExtraParams implements Serializable
	{

		private static final long serialVersionUID = 1L;

		public String[] imageUrls;
		public String[] descs;
		public int extraCurrentItem;

	}

	static final String TAG = "ImageDetailActivity";

	ImagePagerAdapter mAdapter;
	ImageWorker mImageWorker;
	ViewPager mPager;
	ExtraParams extraParams;
	TitleModule titleModule;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thrid_icache_image_detail_pager);
		init();
	}

	void init()
	{
		titleModule = new TitleModule(this, "图片浏览");
		mImageWorker = ICaches.newImageWorker(this, TAG);

		extraParams = (ExtraParams) getIntent().getSerializableExtra(EXTRA_PARAM);
		int extraCurrentItem = extraParams.extraCurrentItem;

		if (extraParams.imageUrls == null)
		{
			McToastUtil.show("imageUrls is null");
			finish();
		}
		// change by zengxiangxin
		titleModule.setTitle(extraParams.descs[0]);

		mAdapter = new ImagePagerAdapter(getSupportFragmentManager());
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);
		mPager.setOnPageChangeListener(mAdapter);
		mPager.setCurrentItem(extraCurrentItem);

	}

	public void onResume()
	{
		super.onResume();
		mImageWorker.setOnScreen(TAG, true);
		mAdapter.notifyDataSetChanged();
	}

	public void onPause()
	{
		super.onPause();
		mImageWorker.setOnScreen(TAG, false);
	}

	public ImageWorker getImageWorker()
	{
		return mImageWorker;
	}

	class ImagePagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener
	{

		public ImagePagerAdapter(FragmentManager fm)
		{
			super(fm);
		}

		public int getCount()
		{
			return extraParams.imageUrls.length;
		}

		public Fragment getItem(int position)
		{
			return ImageDetailFragment.newInstance(extraParams.imageUrls[position]);
		}

		public void destroyItem(ViewGroup container, int position, Object object)
		{
			ImageDetailFragment fragment = (ImageDetailFragment) object;
			fragment.cancelWork();
			super.destroyItem(container, position, object);
		}

		public void onPageScrollStateChanged(int arg0)
		{

		}

		public void onPageScrolled(int arg0, float arg1, int arg2)
		{

		}

		public void onPageSelected(int arg0)
		{
			String title = null;
			if (extraParams.descs != null)
			{
				title = extraParams.descs[arg0];
			} else
			{
				// TODO 图片标题
				// title = extraParams.imageUrls[arg0];
			}
			if (!TextUtils.isEmpty(title))
			{
				titleModule.setTitle(title);
			}
		}
	}

	public static class ImageDetailFragment extends Fragment
	{
		static final String IMAGE_DATA_EXTRA = "url";

		String imageUrl;
		ImageView mImageView;
		ImageWorker mImageWorker;

		public static ImageDetailFragment newInstance(String url)
		{
			ImageDetailFragment f = new ImageDetailFragment();
			Bundle args = new Bundle();
			args.putString(IMAGE_DATA_EXTRA, url);
			f.setArguments(args);
			return f;
		}

		public ImageDetailFragment()
		{
		}

		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			imageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : "";
			View v = inflater.inflate(R.layout.thrid_icache_image_detail_fragment, container, false);
			return v;
		}

		public void onActivityCreated(Bundle savedInstanceState)
		{
			super.onActivityCreated(savedInstanceState);
			if (ImageDetailActivity.class.isInstance(getActivity()) && !TextUtils.isEmpty(imageUrl))
			{
				mImageWorker = ((ImageDetailActivity) getActivity()).getImageWorker();
				mImageWorker.loadBitmap(imageUrl, mImageView);
			}
		}

		public void cancelWork()
		{
			ImageWorker.cancelWork(mImageView);
			mImageView.setImageDrawable(null);
			mImageView = null;
		}
	}
}
